<?php

if (! file_exists(realpath(dirname(__FILE__)) . '/../vendor/autoload.php'))
    exit("autoload文件不存在");

require realpath(dirname(__FILE__)) . '/../vendor/autoload.php';

use paySdk\Pay;
use paySdk\functions\postar\lib\Request\Body\Tag\UnionPayTag;
use paySdk\functions\postar\lib\Request\Body\Order\UnionPayOrder;

$client_ip = "127.0.0.1";
$mobile = "13333333333";
$store_id = 1;

// 将union.html的完整url转换成二维码, 显示在机器上. 
// 用户用云闪付扫描该连接, 云闪付app的内置浏览器会指定一个302跳转到该页面
// 并将userAuthCode、respCode等其他参数以get的方式传进来
// $params为respCode和userAuthCode参数
$params = [
    "respCode" => "00", "userAuthCode" => "EUzEp11ySjO7mGSaJcYgSQ=="
];
// 该参数要从user-agent中获取, 具体获取规则可参考文档
$payCode = "UnionPay/1.0 CloudPay";
if (! isset($params["respCode"]) || $params["respCode"] !== "00")
    throw new Exception("respCode不存在或respCode不为\"00\"");
if (! isset($params["userAuthCode"]) || empty($params["userAuthCode"]))
    throw new Exception("userAuthCode获取失败");

try {
    // 获取银联用户的open_id
    $union_pay_tag_param = [
        "agent_id" => "61000000102111", "customer_id" => "60000000921011",
        "client_ip" => $client_ip, "pay_code" => $payCode, "auth_code" => $params["userAuthCode"],
    ];
    $pay = new Pay(['tag' => new UnionPayTag($union_pay_tag_param), "mode" => "testing"]);

    /* -- 日志与平台公钥可根据自己的需求重新设置路径，如果不设置，默认将日志放在/paySdk/log里，将平台公钥放在/paySdk/pem/postar下 -- */
    /*// 设置日志路径
    $pay->postar->setLog("/path/to/paysdk_log.log");
    // 设置平台公钥证书路径
    $pay->postar->setPlatformPublicKeyPath("/path/to/postar_platform_public_key.pem");*/

    $union_user_info = $pay->postar->tag();
    $union_user_info = json_decode($union_user_info, true);
    if (! isset($union_user_info["code"]) || $union_user_info["code"] !== "000000")
        throw new Exception($union_user_info["msg"]);
    if (! isset($union_user_info["data"]["atqTag"]) || ! isset($union_user_info["data"]["userId"]))
        throw new Exception("不存在atqTag或userId");
    $atq_tag = $union_user_info["data"]["atqTag"];
    $union_open_id = $union_user_info["data"]["userId"];
} catch (\Throwable $e) {
    echo "error msg: {$e->getMessage()}, file: {$e->getFile()}, line: {$e->getLine()} <br />";
    echo "error tracer: {$e->getTraceAsString()} <br />";
}

try{
    // 生成调用统一下单接口参数, 并调用接口
    $union_pay_params = [
        "agent_id" => "61000000102111", "customer_id" => "60000000921011",
        "order_no" => date("YmdHis", time()), "unionPay", "device_no" => "设备号", "tx_amt" => "1",
        "tx_amt_order" => "1", "atq_tag" => $atq_tag, "customer_login" => $mobile,
        "client_ip" => $client_ip,
        "store_id" => $store_id,
        "pay_code" => $payCode,
        "user_auth_code" => $params["userAuthCode"],
        "open_id" => $union_open_id
    ];
    
    $pay = new Pay(["order" => new UnionPayOrder($union_pay_params), "mode" => "testing"]);

    /* -- 日志与平台公钥可根据自己的需求重新设置路径，如果不设置，默认将日志放在/paySdk/log里，将平台公钥放在/paySdk/pem/postar下 -- */
    /*// 设置日志路径
    $pay->postar->setLog("/path/to/paysdk_log.log");
    // 设置平台公钥证书路径
    $pay->postar->setPlatformPublicKeyPath("/path/to/postar_platform_public_key.pem");*/

    $res = $pay->postar->pay();

    var_dump($res);
} catch (\Throwable $e) {
    echo "error msg: {$e->getMessage()}, file: {$e->getFile()}, line: {$e->getLine()} <br />";
    echo "error tracer: {$e->getTraceAsString()} <br />";
}